<?php

define(C("ACCESSACTION"), true);
import('jinghuan.AdminAction');
import('jinghuan.Func');

/**
 * Class ApiAction
 */
class SourceClientAction extends AdminAction
{
    const pwd_str_format = '%s|%s|www.jinghuan.info';

    function datatable()
    {
        $post = I('post.');
        $post["where"]['status'] = 1;
        $count = D(JH_SOURCE_CLIENT)->where($post["where"])->count();
        $row = D(JH_SOURCE_CLIENT)
            ->page($post["page"], $post["length"])
            ->where($post["where"])
            ->order('id desc')
            ->select();

        $this->callback(array(
            'rows' => $row,
            'echo' => $post["echo"],
            'count' => $count
        ));
    }

    function edit()
    {
        $data = D(JH_SOURCE_CLIENT)->find(intval($_POST['id']));
        $this->callback($data);
    }

    /**
     *
     */
    function do_add()
    {
        $post = I('post.');
        $pwd = md5(sprintf($this::pwd_str_format, $post['user_name'], $post['pwd']));

        $count = D(JH_SOURCE_CLIENT)->where('account=\'%s\' or user_name=\'%s\'',[$post['account'],$post['user_name']])->count();

        if ($count>0){
            $this->callback(false,null,"用户名或手机号存在！");
            return;
        }
        $result = D(JH_SOURCE_CLIENT)->add(array(
            "user_name" => $post['user_name'],
            "pwd" => $pwd,
            "name" => $post['name'],
            "account" => $post['account'],
            "sex" => $post['sex'],
            "address" => $post['address'],
            "lift" => $post['lift'],
            "time" => time(),
            "u_time" => time(),
            "status" => 1,
            'uid' => $this->guid(),
        ));
        $this->callback($result != false);
    }

    function do_edit()
    {
        $post = I('post.');
        $arr = array(
            "id" => $post['id'],
            "user_name" => $post['user_name'],
            "name" => $post['name'],
            "account" => $post['account'],
            "sex" => $post['sex'],
            "address" => $post['address'],
            "lift" => $post['lift'],
            "time" => time(),
            "u_time" => time(),
            "status" => 1
        );
        if ($post['pwd']) {
            $pwd = md5(sprintf($this::pwd_str_format, $post['user_name'], $post['pwd']));
            $arr['pwd'] = $pwd;
        }

//        $count = D(JH_SOURCE_CLIENT)->where('account=\'%s\' or user_name=\'%s\'',[$post['account'],$post['user_name']])->count();
//
//        if ($count>0){
//            $this->callback(false,null,"用户名或手机号存在！");
//            return;
//        }

        D(JH_SOURCE_CLIENT)->save($arr);
        $data = D(JH_SOURCE_CLIENT)->find(intval($_POST['id']));
        $this->callback($data,[a=>$pwd]);
    }

    /**
     *
     */
    function do_delete()
    {
        $id = I('post.id');
        $row = D(JH_SOURCE_CLIENT)
            ->where(array('id' => $id))
            ->delete();
        if ($row === false) {
            $this->callback(false);
        } else {
            $this->callback(true);
        }
    }

    public function guid()
    {
        $charid = strtoupper(md5(uniqid(mt_rand(), true)));
//        $hyphen = chr(45); // "-"
        $uuid = substr($charid, 0, 8)
            . substr($charid, 8, 4)
            . substr($charid, 12, 4)
            . substr($charid, 16, 4)
            . substr($charid, 20, 12);
        return $uuid;
    }

    function do_reset()
    {
        $post = I('post.');
        $arr = array(
            "id" => $post['id'],
            "u_time" => time(),
        );
            $pwd = md5(sprintf($this::pwd_str_format, $post['user_name'], '000000'));
            $arr['pwd'] = $pwd;

        D(JH_SOURCE_CLIENT)->save($arr);
        $data = D(JH_SOURCE_CLIENT)->find(intval($_POST['id']));
        $this->callback($data,[a=>$pwd]);
    }

}